package co.sanduche.behave.views;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import co.sanduche.behave.BehaveApp;
import co.sanduche.behave.R;
import co.sanduche.behave.presenters.LoginPresenter;
import co.sanduche.services.EndPoints;
import co.sanduche.util.EPCallback;
import co.sanduche.util.GCMRegistrationManager;
import co.sanduche.vos.LoginCredentials;

public class LoginFragment extends Fragment implements Button.OnClickListener,
		LoginView {

	private EditText identityDocumentField;
	private EditText passwordField;
	private LoginCredentials lc = new LoginCredentials() {
		{

			this.rememberMe = true;
			this.role = "ACUDIENTE";
		}
	};
	private ProgressBar progressBar;

	private LoginPresenter presenter;

	public LoginFragment() {
	}

	public LoginCredentials getCredentials() {
		lc.setPassword(passwordField.getText().toString());
		lc.setUsername(identityDocumentField.getText().toString());
		return lc;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.login_fragment, container,
				false);

		identityDocumentField = ((EditText) rootView
				.findViewById(R.id.identity_document));
		passwordField = ((EditText) rootView.findViewById(R.id.password));
		progressBar = (ProgressBar) rootView
				.findViewById(R.id.progressBarLogin);
		Button button = ((Button) rootView.findViewById(R.id.login_button));
		button.setOnClickListener(this);
		presenter = new LoginPresenter(this);
		return rootView;
	}

	@Override
	public void onClick(View v) {

		progressBar.setVisibility(View.VISIBLE);
		presenter.login(getCredentials());
		// UserEP.login(getCredentials(),
		// new JsonObjectRequestHandler<Void>() {
		//
		// @Override
		// public void onSuccess2(int status, Header[] headers,
		// Void result) {
		//
		// Intent intent = new Intent(getActivity(),
		// MainActivity.class);
		// intent.putExtra("daMesach", "alskdjasldkj");
		// startActivity(intent);
		// getActivity().finish();
		// }
		//
		// @Override
		// public void onFailure(final int status,
		// Header[] headers, String result, Throwable error) {
		// // super.onFailure(status, headers, result, error);
		// progressBar.setVisibility(View.INVISIBLE);
		// Resources r = getResources();
		// String credenciales = r
		// .getString(R.string.invalid_credencials);
		// String servidor = r
		// .getString(R.string.server_not_working);
		// String message = status == 401 ? credenciales
		// : servidor;
		//
		// DialogUtil.alert(LoginFragment.this, message);
		//
		// }
		// });

	}

	@Override
	public void goToStudentsActivity() {

	}

	@Override
	public void goToStudentActivity(Long idEstudiante) {
		Log.i(getClass().getSimpleName(),
				"Logeado correctamente, registrando android id");
		GCMRegistrationManager.register(getActivity(),
				new EPCallback<String>() {

					@Override
					public void onSuccess(String registrationID) {
						EndPoints.UserEP.saveAndroidId(registrationID,
								new EPCallback<Void>() {

									@Override
									public void onSuccess(Void arg0) {
										Log.i("RegistrationID",
												"RegistrationID almacenado correctamente");
									}
								});

					}
				});

		Intent i = new Intent(getActivity(), MainActivity.class);
		i.putExtra(BehaveApp.ID_ESTUDIANTE_EXTRA, idEstudiante);
		startActivity(i);
		getActivity().finish();
	}
}
